package com.yhp.service.impl.userserviceimpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yhp.enums.OrderStatusEnum;
import com.yhp.enums.YesOrNo;
import com.yhp.mapper.ItemsCommentsMapperCustom;
import com.yhp.mapper.OrderStatusMapper;
import com.yhp.mapper.OrdersMapper;
import com.yhp.mapper.centermapper.OrdersMapperCustom;
import com.yhp.pojo.OrderStatus;
import com.yhp.pojo.Orders;
import com.yhp.service.BaseService;
import com.yhp.service.userservice.CenterOrderService;
import com.yhp.utils.PagedGridResult;
import com.yhp.vo.MyOrderVO;
import com.yhp.vo.OrderStatusCountsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.annotation.Order;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CenterOrderServiceImpl extends BaseService implements CenterOrderService {
    @Autowired
    private OrdersMapperCustom ordersMapperCustom;
    @Autowired
    private OrderStatusMapper orderStatusMapper;

    @Autowired
    private OrdersMapper ordersMapper;


    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public PagedGridResult queryMyOrders(String userId, String orderStatus, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);

        Map map=new HashMap();
        map.put("userId",userId);
        map.put("orderStatus",orderStatus);
        List<MyOrderVO> myOrderVOS = ordersMapperCustom.queryMyOrders(map);


        return setPagedGrid(myOrderVOS,page);
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateDeliverOrderStatus(String orderId) {
        OrderStatus orderStatus = new OrderStatus();
        orderStatus.setOrderStatus(OrderStatusEnum.WAIT_RECEIVE.type);
        orderStatus.setDeliverTime(new Date());

        Example example = new Example(OrderStatus.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderId",orderId);
        criteria.andEqualTo("orderStatus",OrderStatusEnum.WAIT_DELIVER.type);

        orderStatusMapper.updateByExampleSelective(orderStatus,example);
    }

    @Override
    public Orders queryMyOneOrder(String userId, String orderId) {

        Example example = new Example(Orders.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",orderId);
        criteria.andEqualTo("userId",userId);
        criteria.andEqualTo("isDelete", YesOrNo.NO.type);
        Orders orders = ordersMapper.selectOneByExample(example);
        return orders;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updateOrderStatusReceived(String orderId) {
        OrderStatus orderStatus = new OrderStatus();
        orderStatus.setOrderStatus(OrderStatusEnum.SUCCESS.type);
        orderStatus.setSuccessTime(new Date());

        Example example = new Example(OrderStatus.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderId",orderId);
        criteria.andEqualTo("orderStatus",OrderStatusEnum.WAIT_RECEIVE.type);

        int i = orderStatusMapper.updateByExampleSelective(orderStatus, example);

        return i>0?true:false;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updateOrderStausDeleted(String userId, String orderId) {
        Orders orders = new Orders();
        orders.setIsDelete(YesOrNo.YES.type);
        orders.setUpdatedTime(new Date());

        Example example = new Example(Orders.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",orderId);
        criteria.andEqualTo("userId",userId);

        int i = ordersMapper.updateByExampleSelective(orders, example);
        return i>0?true:false;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public OrderStatusCountsVO getOrderStatusCount(String userId) {
            Map map=new HashMap();
            map.put("userId",userId);
            map.put("orderStatus",OrderStatusEnum.WAIT_PAY.type);
            //待付款数量
        int waitPay = ordersMapperCustom.getMyOrderStatusCount(map);
        //已付款
        map.put("orderStatus",OrderStatusEnum.WAIT_DELIVER.type);
        int waitDeliver = ordersMapperCustom.getMyOrderStatusCount(map);

        //已发货
        map.put("orderStatus",OrderStatusEnum.WAIT_RECEIVE.type);
        int wait_receive = ordersMapperCustom.getMyOrderStatusCount(map);

        //交易成功，未点评
        map.put("orderStatus",OrderStatusEnum.SUCCESS.type);
        map.put("isComment",YesOrNo.NO.type);
        int success = ordersMapperCustom.getMyOrderStatusCount(map);

        OrderStatusCountsVO orderStatusCountsVO = new OrderStatusCountsVO();
        orderStatusCountsVO.setWaitPayCounts(waitPay);
        orderStatusCountsVO.setWaitCommentCounts(success);
        orderStatusCountsVO.setWaitDeliverCounts(waitDeliver);
        orderStatusCountsVO.setWaitReceiveCounts(wait_receive);

        return orderStatusCountsVO;
    }

    @Override
    public PagedGridResult getOrderTrend(String userId, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);

        Map map=new HashMap();
        map.put("userId",userId);
        List<OrderStatus> myOrderTrend = ordersMapperCustom.getMyOrderTrend(map);
        return setPagedGrid(myOrderTrend,page);
    }
}
